resolver.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import functools
  2. import logging
  3. from pip._vendor import six
  4. from pip._vendor.packaging.utils import canonicalize_name
  5. from pip._vendor.resolvelib import BaseReporter, ResolutionImpossible
  6. from pip._vendor.resolvelib import Resolver as RLResolver
  7. from pip._internal.exceptions import InstallationError
  8. from pip._internal.req.req_install import check_invalid_constraint_type
  9. from pip._internal.req.req_set import RequirementSet
  10. from pip._internal.resolution.base import BaseResolver
  11. from pip._internal.resolution.resolvelib.provider import PipProvider
  12. from pip._internal.utils.misc import dist_is_editable
  13. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  14. from .factory import Factory
  15. if MYPY_CHECK_RUNNING:
  16. from typing import Dict, List, Optional, Set, Tuple
  17. from pip._vendor.packaging.specifiers import SpecifierSet
  18. from pip._vendor.resolvelib.resolvers import Result
  19. from pip._vendor.resolvelib.structs import Graph
  20. from pip._internal.cache import WheelCache
  21. from pip._internal.index.package_finder import PackageFinder
  22. from pip._internal.operations.prepare import RequirementPreparer
  23. from pip._internal.req.req_install import InstallRequirement
  24. from pip._internal.resolution.base import InstallRequirementProvider
  25. logger = logging.getLogger(__name__)
  26. class Resolver(BaseResolver):
  27. _allowed_strategies = {"eager", "only-if-needed", "to-satisfy-only"}
  28. def __init__(
  29. self,
  30. preparer, # type: RequirementPreparer
  31. finder, # type: PackageFinder
  32. wheel_cache, # type: Optional[WheelCache]
  33. make_install_req, # type: InstallRequirementProvider
  34. use_user_site, # type: bool
  35. ignore_dependencies, # type: bool
  36. ignore_installed, # type: bool
  37. ignore_requires_python, # type: bool
  38. force_reinstall, # type: bool
  39. upgrade_strategy, # type: str
  40. py_version_info=None, # type: Optional[Tuple[int, ...]]
  41. lazy_wheel=False, # type: bool
  42. ):
  43. super(Resolver, self).__init__()
  44. if lazy_wheel:
  45. logger.warning(
  46. 'pip is using lazily downloaded wheels using HTTP '
  47. 'range requests to obtain dependency information. '
  48. 'This experimental feature is enabled through '
  49. '--use-feature=fast-deps and it is not ready for production.'
  50. )
  51. assert upgrade_strategy in self._allowed_strategies
  52. self.factory = Factory(
  53. finder=finder,
  54. preparer=preparer,
  55. make_install_req=make_install_req,
  56. wheel_cache=wheel_cache,
  57. use_user_site=use_user_site,
  58. force_reinstall=force_reinstall,
  59. ignore_installed=ignore_installed,
  60. ignore_requires_python=ignore_requires_python,
  61. py_version_info=py_version_info,
  62. lazy_wheel=lazy_wheel,
  63. )
  64. self.ignore_dependencies = ignore_dependencies
  65. self.upgrade_strategy = upgrade_strategy
  66. self._result = None # type: Optional[Result]
  67. def resolve(self, root_reqs, check_supported_wheels):
  68. # type: (List[InstallRequirement], bool) -> RequirementSet
  69. constraints = {} # type: Dict[str, SpecifierSet]
  70. user_requested = set() # type: Set[str]
  71. requirements = []
  72. for req in root_reqs:
  73. if req.constraint:
  74. # Ensure we only accept valid constraints
  75. problem = check_invalid_constraint_type(req)
  76. if problem:
  77. raise InstallationError(problem)
  78. if not req.match_markers():
  79. continue
  80. name = canonicalize_name(req.name)
  81. if name in constraints:
  82. constraints[name] = constraints[name] & req.specifier
  83. else:
  84. constraints[name] = req.specifier
  85. else:
  86. if req.user_supplied and req.name:
  87. user_requested.add(canonicalize_name(req.name))
  88. r = self.factory.make_requirement_from_install_req(
  89. req, requested_extras=(),
  90. )
  91. if r is not None:
  92. requirements.append(r)
  93. provider = PipProvider(
  94. factory=self.factory,
  95. constraints=constraints,
  96. ignore_dependencies=self.ignore_dependencies,
  97. upgrade_strategy=self.upgrade_strategy,
  98. user_requested=user_requested,
  99. )
  100. reporter = BaseReporter()
  101. resolver = RLResolver(provider, reporter)
  102. try:
  103. try_to_avoid_resolution_too_deep = 2000000
  104. self._result = resolver.resolve(
  105. requirements, max_rounds=try_to_avoid_resolution_too_deep,
  106. )
  107. except ResolutionImpossible as e:
  108. error = self.factory.get_installation_error(e)
  109. six.raise_from(error, e)
  110. req_set = RequirementSet(check_supported_wheels=check_supported_wheels)
  111. for candidate in self._result.mapping.values():
  112. ireq = candidate.get_install_requirement()
  113. if ireq is None:
  114. continue
  115. # Check if there is already an installation under the same name,
  116. # and set a flag for later stages to uninstall it, if needed.
  117. # * There isn't, good -- no uninstalltion needed.
  118. # * The --force-reinstall flag is set. Always reinstall.
  119. # * The installation is different in version or editable-ness, so
  120. # we need to uninstall it to install the new distribution.
  121. # * The installed version is the same as the pending distribution.
  122. # Skip this distrubiton altogether to save work.
  123. installed_dist = self.factory.get_dist_to_uninstall(candidate)
  124. if installed_dist is None:
  125. ireq.should_reinstall = False
  126. elif self.factory.force_reinstall:
  127. ireq.should_reinstall = True
  128. elif installed_dist.parsed_version != candidate.version:
  129. ireq.should_reinstall = True
  130. elif dist_is_editable(installed_dist) != candidate.is_editable:
  131. ireq.should_reinstall = True
  132. else:
  133. continue
  134. link = candidate.source_link
  135. if link and link.is_yanked:
  136. # The reason can contain non-ASCII characters, Unicode
  137. # is required for Python 2.
  138. msg = (
  139. u'The candidate selected for download or install is a '
  140. u'yanked version: {name!r} candidate (version {version} '
  141. u'at {link})\nReason for being yanked: {reason}'
  142. ).format(
  143. name=candidate.name,
  144. version=candidate.version,
  145. link=link,
  146. reason=link.yanked_reason or u'<none given>',
  147. )
  148. logger.warning(msg)
  149. req_set.add_named_requirement(ireq)
  150. return req_set
  151. def get_installation_order(self, req_set):
  152. # type: (RequirementSet) -> List[InstallRequirement]
  153. """Get order for installation of requirements in RequirementSet.
  154. The returned list contains a requirement before another that depends on
  155. it. This helps ensure that the environment is kept consistent as they
  156. get installed one-by-one.
  157. The current implementation creates a topological ordering of the
  158. dependency graph, while breaking any cycles in the graph at arbitrary
  159. points. We make no guarantees about where the cycle would be broken,
  160. other than they would be broken.
  161. """
  162. assert self._result is not None, "must call resolve() first"
  163. graph = self._result.graph
  164. weights = get_topological_weights(graph)
  165. sorted_items = sorted(
  166. req_set.requirements.items(),
  167. key=functools.partial(_req_set_item_sorter, weights=weights),
  168. reverse=True,
  169. )
  170. return [ireq for _, ireq in sorted_items]
  171. def get_topological_weights(graph):
  172. # type: (Graph) -> Dict[Optional[str], int]
  173. """Assign weights to each node based on how "deep" they are.
  174. This implementation may change at any point in the future without prior
  175. notice.
  176. We take the length for the longest path to any node from root, ignoring any
  177. paths that contain a single node twice (i.e. cycles). This is done through
  178. a depth-first search through the graph, while keeping track of the path to
  179. the node.
  180. Cycles in the graph result would result in node being revisited while also
  181. being it's own path. In this case, take no action. This helps ensure we
  182. don't get stuck in a cycle.
  183. When assigning weight, the longer path (i.e. larger length) is preferred.
  184. """
  185. path = set() # type: Set[Optional[str]]
  186. weights = {} # type: Dict[Optional[str], int]
  187. def visit(node):
  188. # type: (Optional[str]) -> None
  189. if node in path:
  190. # We hit a cycle, so we'll break it here.
  191. return
  192. # Time to visit the children!
  193. path.add(node)
  194. for child in graph.iter_children(node):
  195. visit(child)
  196. path.remove(node)
  197. last_known_parent_count = weights.get(node, 0)
  198. weights[node] = max(last_known_parent_count, len(path))
  199. # `None` is guaranteed to be the root node by resolvelib.
  200. visit(None)
  201. # Sanity checks
  202. assert weights[None] == 0
  203. assert len(weights) == len(graph)
  204. return weights
  205. def _req_set_item_sorter(
  206. item, # type: Tuple[str, InstallRequirement]
  207. weights, # type: Dict[Optional[str], int]
  208. ):
  209. # type: (...) -> Tuple[int, str]
  210. """Key function used to sort install requirements for installation.
  211. Based on the "weight" mapping calculated in ``get_installation_order()``.
  212. The canonical package name is returned as the second member as a tie-
  213. breaker to ensure the result is predictable, which is useful in tests.
  214. """
  215. name = canonicalize_name(item[0])
  216. return weights[name], name